/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.block;

import org.statewalker.parser.wiki.tokenizer.base.StreamToken;
import org.statewalker.tokenizer.base.AbstractTokenizer;
import org.statewalker.tokenizer.base.ITokenFactory;
import org.statewalker.tokenizer.base.NewLineTokenizer;
import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.IMarker;

public class ParagraphTokenizer extends AbstractTokenizer<StreamToken> {

    private NewLineTokenizer<StreamToken> fNewLineTokenizer;

    public ParagraphTokenizer(
        ITokenFactory<StreamToken> factory,
        NewLineTokenizer<StreamToken> newLineTokenizer) {
        super(factory);
        fNewLineTokenizer = newLineTokenizer;
    }

    public StreamToken read(ICharStream stream) {
        StreamToken result = null;
        IMarker marker = stream.markPosition();
        try {
            int counter = 0;
            while (fNewLineTokenizer.read(stream) != null) {
                counter++;
            }
            result = counter > 0 ? newToken(stream, marker) : null;
            return result;
        } finally {
            marker.close(result == null);
        }
    }
}